package com.example.wwk.graduationpractice.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.wwk.graduationpractice.AsyncTask.LoginAsyncTask;
import com.example.wwk.graduationpractice.Globalutils.SharedPreferenceUtils;
import com.example.wwk.graduationpractice.R;

import org.litepal.tablemanager.Connector;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    //back
    private ImageView back;
    //忘记密码
    private TextView forgetPassword;
    //editText
    private EditText passwordText;
    private EditText userText;
    //button
    private Button login;
    private Button register;
    //toobar
    private Toolbar toolbar;
    //记住密码
    private CheckBox rememberPwd;
    private boolean isRememberPwd;      //判断是否记住了密码
    private SharedPreferences pref;     //存储
    private SharedPreferences.Editor editor;
    //选择登录身份
    public static final int DOCTOR = 111;
    public static final int USER  = 222;
    private int IDENTITY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        setTitle("");
        //toolbar
        toolbar = (Toolbar) findViewById(R.id.login_toolbar);
        setSupportActionBar(toolbar);
        //忘记密码
        forgetPassword = (TextView) findViewById(R.id.login_forget_password);
        forgetPassword.setOnClickListener(this);
        //back
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(this);
        //登录按钮
        login = (Button) findViewById(R.id.login_login);
        login.setOnClickListener(this);
        //注册用户按钮
        register = (Button) findViewById(R.id.login_register);
        register.setOnClickListener(this);
        //editText username和password
        passwordText = (EditText) findViewById(R.id.login_password);
        userText = (EditText) findViewById(R.id.login_user_name);
        //记住密码
        rememberPwd = (CheckBox) findViewById(R.id.login_remember_password);
        pref = SharedPreferenceUtils.getSharedPreferences();
        initCheckBox();   //当checkBox记住了密码;默认初始为false
        //数据库
        Connector.getDatabase();//创建（获得）litepal数据库
        //spinner 选择登录身份
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {

                String[] identitys = getResources().getStringArray(R.array.identity);
                if(identitys[pos].equals("咨询者")){
                    IDENTITY = USER;
                }else{
                    IDENTITY = DOCTOR;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_forget_password:
                Intent intent2 = new Intent(LoginActivity.this, FindPasswordActivity.class);
                startActivity(intent2);
                break;
            case R.id.login_register:
                final Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
            case R.id.login_login:
                //获得editText内容
                String user = userText.getText().toString();
                String pwd = passwordText.getText().toString();
                boolean check = rememberPwd.isChecked();
                //登录处理
                LoginAsyncTask.login(IDENTITY,LoginActivity.this, pref, editor, check, user, pwd);
                break;

            default:
                break;
        }
    }
    /*
    * 初始化checkBox
    */
    private void initCheckBox() {
        //通过SharedPreferences的对象的方法获取remember_password该键的对应值，一开始默认为否即没有对应值
        isRememberPwd = pref.getBoolean("remember_password", false);
        //当记住了密码
        if (isRememberPwd) {
            //将用户名和密码设置到输入框
            String user = pref.getString("user", "");
            String password = pref.getString("password", "");
            userText.setText(user);
            passwordText.setText(password);
            rememberPwd.setChecked(true);
        }
    }


}
